import 'dart:ui';

import 'package:flutter/animation.dart';
import 'package:flutter/cupertino.dart';
import 'package:go_router/go_router.dart';

import 'router_key.dart';


class Handler {
  ///页面键
  final RouterKey routerKey;

  ///构建页面视图回调
  final GoRouterWidgetBuilder? handlerFunc;

  ///当前路由重定向拦截器
  final GoRouterRedirect? redirect;

  ///动画类型
  final AnimationType animationType;

  ///子路由列表
  final List<Handler> childHandlers;

  ///路由跳转是是否遮挡前一页面
  final bool opaque;

  Handler(this.routerKey,
      {this.handlerFunc,
      this.redirect,
      this.animationType = AnimationType.slideLeft,
      this.childHandlers = const <Handler>[],
      this.opaque = true});
}

enum AnimationType {
  slideLeft, slideBottom, fadeIn, none;

  Widget getAnimation(context, animation, secondaryAnimation, child) {
    switch (this) {
      case AnimationType.slideLeft:
        return _slideLeft(context, animation, secondaryAnimation, child);
      case AnimationType.slideBottom:
        return _slideBottom(context, animation, secondaryAnimation, child);
      case AnimationType.fadeIn:
        return _fadeIn(context, animation, secondaryAnimation, child);
      case AnimationType.none:
        return _slideLeft(context, animation, secondaryAnimation, child);
    }
  }
  /// 从底部进入
  Widget _slideBottom(context, animation, secondaryAnimation, child) {
    const begin = Offset(0.0, 1.0);
    return _slideAnimation(begin, animation, child);
  }
  /// 从右侧向左滑入
  Widget _slideLeft(context, animation, secondaryAnimation, child) {
    const begin = Offset(1.0, 0.0);
    return _slideAnimation(begin, animation, child);
  }
  /// 淡入淡出
  Widget _fadeIn(context, animation, secondaryAnimation, child) {
    return FadeTransition(
      opacity: animation,
      child: child,
    );
  }

  SlideTransition _slideAnimation(Offset begin, animation, child) {
    const end = Offset.zero;
    const curve = Curves.easeInOut;

    final tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
    final offsetAnimation = animation.drive(tween);

    return SlideTransition(
      position: offsetAnimation,
      child: child,
    );
  }

}
